﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Pibby.Core.IO.Tests.Stubs
{
	public class Folder : IFolder
	{
		#region Constructors

		public Folder(string path)
			: this(path, 0, 0)
		{
		}

		public Folder(Environment.SpecialFolder specialFolder)
			: this(Environment.GetFolderPath(specialFolder))
		{
		}

		public Folder(string path, int files)
			: this(path, files, 0)
		{
		}

		public Folder(string path, int files, int folders)
		{
			this.Path = path;

			if (files > 0)
				this.HowManyFiles = files;

			if (folders > 0)
				this.HowManyFolders = folders;

			this.FileExtension = "mp3";

			this.Files = new IFileCollection();
			this.Folders = new IFolderCollection();

			this.CreateSubFolders();
			this.CreateFiles();
		}

		public Folder(Folder parent, string path)
			: this(path, parent.HowManyFiles - 1, parent.HowManyFolders - 1)
		{
		}

		#endregion Constructors

		#region Properties

		public bool Exists { get { return true; } }

		public string Path
		{
			get;
			set;
		}

		public bool AutoCreate
		{
			get;
			set;
		}

		public int HowManyFiles
		{
			get;
			private set;
		}

		public int HowManyFolders
		{
			get;
			private set;
		}

		public string Root
		{
			get
			{
				return System.IO.Path.GetPathRoot(this.Path);
			}
		}

		public string FileExtension
		{
			get;
			set;
		}

		private IFolderCollection Folders
		{
			get;
			set;
		}

		private IFileCollection Files
		{
			get;
			set;
		}

		#endregion Properties

		#region Methods

		public IFolderCollection GetSubfolders()
		{
			return this.Folders;
		}

		public IFileCollection GetFiles()
		{
			return this.Files;
		}

		public IFile ToFile(string file)
		{
			return new Stubs.File(System.IO.Path.Combine(this.Path, file));
		}

		public IFolder Parent
		{
			get
			{
				return new Stubs.Folder(System.IO.Path.Combine(this.Path, ".."));
			}
		}

		public IFileCollection GetFiles(string searchMask)
		{
			string extension = "mp3";

			if (searchMask != "*.*")
				extension = searchMask.Substring(2);

			this.Files.Clear();

			for (int i = 0; i < this.HowManyFiles; ++i)
			{
				this.Files.Add(new File()
				{
					Path = string.Format("{0}\\File{1}.{2}", this.Path, i + 1, extension),
					Length = (i + 1) * 1024
				});
			}

			return this.Files;
		}

		public IFolder Append(string path)
		{
			Folder subFolder = new Folder(string.Format("{0}\\{1}", this.Path, path));

			this.Folders.Add(subFolder);

			return subFolder;
		}

		public string PathToFile(string fileName)
		{
			return System.IO.Path.Combine(this.Path, fileName);
		}

		public void Create()
		{
		}

		public void Delete()
		{
			this.Files.Clear();
			this.Folders.Clear();
		}

		#endregion Methods

		private void CreateSubFolders()
		{
			for (int i = 0; i < this.HowManyFolders; ++i)
			{
				this.Folders.Add(new Folder(this, string.Format("{0}\\Folder{1}", this.Path, i + 1)));
			}
		}

		private void CreateFiles()
		{
			for (int i = 0; i < this.HowManyFiles; ++i)
			{
				this.Files.Add(new File()
				{
					Path = string.Format("{0}\\File{1}.{2}", this.Path, i + 1, this.FileExtension),
					Length = (i + 1) * 1024
				});
			}
		}


		public string Name
		{
			get { throw new NotImplementedException(); }
		}
	}
}
